/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 *   All Rights Reserved. See end of file
 */
package com.bmackay.maven.publisher;

import com.bmackay.semweb.commons.decorators.gloze.GlozeDecorator;
import com.bmackay.semweb.commons.decorators.jena.LocationMapperModel;
import com.bmackay.semweb.commons.decorators.jena.writer.ModelWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.hp.hpl.jena.rdf.model.Model;

public class App extends SimpleHTML {

    private static GlozeDecorator glozeDecorator = new GlozeDecorator();
    private static LocationMapperModel locationMapperModel = new LocationMapperModel();

    public static void main(String[] args) {
        TimeStamps.init();
        tableStart();
        create(new File(Constants.mavenrepo));
        fileWrite(Constants.semWebRepoApp + "/index.html", sb.toString());
        tableEnd();
        try {
            locationMapperModel.saveLocationMapper(Constants.semWebRepoApp + "/locations.n3");
        } catch (IOException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
        TimeStamps.updateTimeStamps();
    }

    private static void create(File dir) {

        boolean hasName = false;
        if (dir.isDirectory() == true) {
            trStart();
            for (File file : dir.listFiles()) {
                if (file != null) {

                    if (file.getName().contains(".jar") || file.getName().contains(".pom")) {
                        // System.out.print("TimeStamps.. from \n" + file + "\n");
                        if (TimeStamps.check(file) == false) {

                            // System.out.print(file.getAbsolutePath() + "\n");
                            if (hasName == false) {
                                hasName = true;
                            }
                            String f = file.getAbsolutePath().substring(Constants.mavenrepo.length());
                            href(f, file.getName());


                            if (TimeStamps.copy(file) == true) {
                                try {
                                    String filetoCopy = createLocalPath(file);
                                    if (Constants.liftPomFilesOnly.equals("false")) {

                                        File repo = new File(file.getAbsolutePath());
                                        System.out.print("copying.. from \n" + repo + "\n");
                                        System.out.print("copying.. to \n" + filetoCopy + "\n\n");
                                        copy(repo, new File(filetoCopy));

                                    }

                                    String modelName = filetoCopy + ".n3";

                                    if (file.getName().endsWith(".pom")) {
                                        String entryName = createHyperLink(file) + ".n3";
                                        locationMapperModel.addAltEntry(entryName, file.getName() + ".n3");
                                        Model m = glozeDecorator.lift(file.getAbsolutePath(),
                                                Constants.MAVEN_NAMESPACE,
                                                Constants.XSD_PATH);

                                        ModelWriter.printWriterN3(m, modelName);

                                    }

                                } catch (IOException ex) {
                                    Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        }
                    }
                    create(file);
                }
                trEnd();
            }
        }
    }

    private static String createLocalPath(File file) {
        return Constants.semWebRepoApp + parsePath(file);
    }

    private static String createHyperLink(File file) {
        return Constants.semwebRepoUrl + parsePath(file);
    }

    private static String parsePath(File file) {

        String filetoCopy = file.getAbsolutePath();
        int i = Constants.mavenrepo.length();
        filetoCopy = filetoCopy.substring(i + 1);
        return filetoCopy;
    }

    private static void copy(File src, File dst) throws IOException {

        new File(dst.getAbsolutePath().substring(0, dst.getAbsolutePath().indexOf(dst.getName()))).mkdirs();

        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    public static void fileWrite(String fileName, String content) {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write(content);
            out.close();
        } catch (IOException e) {
        }
    }
}

/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 * All Rights Reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

